/*
 * ============================================================================
 *                   GNU Lesser General Public License
 * ============================================================================
 *
 *
 *
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
 * 
 *
 *
 */
package rad.framework.validator;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;

import rad.framework.lang.DateUtil;

import org.hibernate.mapping.Property;
import org.hibernate.validator.PropertyConstraint;
import org.hibernate.validator.Validator;

/**
 * Check that a given date is not in the past.
 * 
 * @author boudyacho
 */
public class NotPastValidator implements Validator<NotPast>,
		PropertyConstraint, Serializable {
	private static final long serialVersionUID = 1L;

	public void initialize(NotPast parameters) {
	}

	public boolean isValid(Object value) {
		if (value == null)
			return true;
		if (value instanceof Date) {
			Date date = (Date) value;
			// truncate now() otherwise this will always be true
			Date today = DateUtil.truncateDate(DateUtil.now());
			return !date.before(today);
			// TODO calendar
			// } else if (value instanceof Calendar) {
			// Calendar cal = (Calendar) value;
			// return !cal.before(Calendar.getInstance());
		} else {
			return false;
		}
	}

	public void apply(Property property) {
		// Column col = (Column) property.getColumnIterator().next();
		// col.setCheckConstraint(col.getName() + " >= current_date");
	}
}
